package com.example.hello;

import com.example.hello.client.Paragraph.BootstrapParagraphState;
import com.vaadin.server.Page;
import com.vaadin.ui.AbstractComponent;

public class BootstrapParagraph extends AbstractComponent {

    public BootstrapParagraph() {
        this("");
    }

    public BootstrapParagraph(String text) {
        this(text, "TOP_LEFT", 10);
    }

    public BootstrapParagraph(String text, int margin) {
        this(text, "TOP_LEFT", margin);
    }

    public BootstrapParagraph(String text, String alignment) {
        this(text, alignment, 10);
    }

    public BootstrapParagraph(String text, String alignment, int margin) {
        setText(text);
        setAlignment(alignment);
        setMargin(margin);
    }

    @Override
    protected BootstrapParagraphState getState() {
        return (BootstrapParagraphState) super.getState();
    }

    public String getText() {
        return getState().getText();
    }

    public void setText(String text) {
        getState().setText(text);
    }

    public String getAlignment() {
        return getState().getAlignment();
    }

    public void setAlignment(String alignment) {
        Page.Styles styles = Page.getCurrent().getStyles();
        if (!styles.toString().contains(".helloVG .spacing_" + alignment.toLowerCase())) {
            styles.add(".helloVG .spacing_" + alignment.toLowerCase() + " {" +
                    "text-align: " + alignment.split("_")[1] + ";" +
                    "vertical-align: " + alignment.split("_")[0] + "; }");
        }
        getState().setAlignment(alignment);
    }

    public int getMargin() {
        return getState().getMargin();
    }

    public void setMargin(int margin) {
        Page.Styles styles = Page.getCurrent().getStyles();
        if (!styles.toString().contains(".helloVG .margin-" + margin)) {
            styles.add(".helloVG .margin-" + margin + " { margin-bottom: " + margin + "; }");
        }
        getState().setMargin(margin);
    }

}